using System;
using System.Collections.Generic;
using System.Text;

namespace Common.Graphics
{
    public class LabelMover
    {
        public LabelList List;

        public event EventHandler OnRefresh;

        public void Start()
        {
            while (1==1)
            {

                while (List.Stop) 
                {
                    System.Threading.Thread.Sleep(0);
                }

                bool offScreen = false;

                if ((List.LabelPositions[0] < -37) && (List.YDirection == LabelList.DIRECTION_UP))
                {
                    offScreen = true;
                }

                if ((List.LabelPositions[List.LabelPositions.Length -1] > 253) && (List.YDirection == LabelList.DIRECTION_DOWN))
                {
                    offScreen = true;
                }

                int adjustedSpeed = 1;

                if (List.Speed > 300)
                {
                    adjustedSpeed = 25;
                }
                else if (List.Speed > 250)
                {
                    adjustedSpeed = 20;
                }
                else if (List.Speed > 200)
                {
                    adjustedSpeed = 15;
                }
                else if (List.Speed > 100)
                {
                    adjustedSpeed = 10;
                }
                else if (List.Speed > 75)
                {
                    adjustedSpeed = 5;
                }
                else if (List.Speed > 50)
                {
                    adjustedSpeed = 2;
                }

                bool cancel = false;

                for (int i = 0; i < List.LabelPositions.Length; i++)
                {

                    if (i == 0)
                    {
                        if (offScreen)
                        {
                            if (List.YDirection == LabelList.DIRECTION_DOWN)
                            {
                                if (List.CurrentPosition > 0)
                                {
                                    List.LabelPositions[i] = -25;
                                    List.CurrentPosition -= 1;
                                }
                                else
                                {
                                    cancel = true;
                                }
                            }
                            else
                            {
                                if (List.CurrentPosition < (List.ListData.Length - List.LabelPositions.Length)) 
                                {
                                    List.LabelPositions[i] = 0;
                                    List.CurrentPosition += 1;
                                }
                                else
                                {
                                    cancel = true;
                                }
                            }
                        }
                        else
                        {
                            if (!cancel)
                            {
                                if (List.YDirection == LabelList.DIRECTION_UP)
                                {
                                    List.LabelPositions[i] -= adjustedSpeed;
                                }
                                else
                                {
                                    List.LabelPositions[i] += adjustedSpeed;
                                }
                            }
                        }
                    }
                    else
                    {
                        if (!cancel)
                        {
                            List.LabelPositions[i] = List.LabelPositions[i - 1] + 40;
                        }
                    }

                    if (!cancel)
                    {
                        if (offScreen)
                        {
                            if (List.YDirection == LabelList.DIRECTION_UP)
                            {
                                if (i == (List.LabelPositions.Length - 1))
                                {
                                    Common.Audio.Wav.PlaySound(@"plmDefault.wav", IntPtr.Zero, Common.Audio.Wav.SoundFlags.SND_NODEFAULT | Common.Audio.Wav.SoundFlags.SND_FILENAME | Common.Audio.Wav.SoundFlags.SND_ASYNC | Common.Audio.Wav.SoundFlags.SND_NOWAIT);
                                }
                            }
                            else
                            {
                                if (i == 0)
                                {
                                    Common.Audio.Wav.PlaySound(@"plmDefault.wav", IntPtr.Zero, Common.Audio.Wav.SoundFlags.SND_NODEFAULT | Common.Audio.Wav.SoundFlags.SND_FILENAME | Common.Audio.Wav.SoundFlags.SND_ASYNC | Common.Audio.Wav.SoundFlags.SND_NOWAIT);
                                }
                            }
                        }
                    }

                }

                List.AllowPaint = true;
                List.Invoke(this.OnRefresh, new object[0]);
            }


        }
    }
}
